home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_urlparse.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.1 KB  |  95 lines

  1. import urlparse
  2.  
  3. errors = 0
  4.  
  5. RFC1808_BASE = "http://a/b/c/d;p?q#f"
  6.  
  7. for url, expected in [('http://www.python.org',
  8.                        ('http', 'www.python.org', '', '', '', '')),
  9.                       ('http://www.python.org#abc',
  10.                        ('http', 'www.python.org', '', '', '', 'abc')),
  11.                       ('http://www.python.org/#abc',
  12.                        ('http', 'www.python.org', '/', '', '', 'abc')),
  13.                       (RFC1808_BASE,
  14.                        ('http', 'a', '/b/c/d', 'p', 'q', 'f')),
  15.                       ('file:///tmp/junk.txt',
  16.                        ('file', '', '/tmp/junk.txt', '', '', '')),
  17.                       ]:
  18.     result = urlparse.urlparse(url)
  19.     print "%-13s = %r" % (url, result)
  20.     if result != expected:
  21.         errors += 1
  22.         print "urlparse(%r)" % url
  23.         print ("expected %r,\n"
  24.                "     got %r") % (expected, result)
  25.     # put it back together and it should be the same
  26.     result2 = urlparse.urlunparse(result)
  27.     assert(result2 == url)
  28. print
  29.  
  30. def checkJoin(relurl, expected):
  31.     global errors
  32.     result = urlparse.urljoin(RFC1808_BASE, relurl)
  33.     print "%-13s = %r" % (relurl, result)
  34.     if result != expected:
  35.         errors += 1
  36.         print "urljoin(%r, %r)" % (RFC1808_BASE, relurl)
  37.         print ("expected %r,\n"
  38.                "     got %r") % (expected, result)
  39.  
  40. print "urlparse.urljoin() tests"
  41. print
  42.  
  43. # "normal" cases from RFC 1808:
  44. checkJoin('g:h', 'g:h')
  45. checkJoin('g', 'http://a/b/c/g')
  46. checkJoin('./g', 'http://a/b/c/g')
  47. checkJoin('g/', 'http://a/b/c/g/')
  48. checkJoin('/g', 'http://a/g')
  49. checkJoin('//g', 'http://g')
  50. checkJoin('?y', 'http://a/b/c/d;p?y')
  51. checkJoin('g?y', 'http://a/b/c/g?y')
  52. checkJoin('g?y/./x', 'http://a/b/c/g?y/./x')
  53. checkJoin('#s', 'http://a/b/c/d;p?q#s')
  54. checkJoin('g#s', 'http://a/b/c/g#s')
  55. checkJoin('g#s/./x', 'http://a/b/c/g#s/./x')
  56. checkJoin('g?y#s', 'http://a/b/c/g?y#s')
  57. checkJoin(';x', 'http://a/b/c/d;x')
  58. checkJoin('g;x', 'http://a/b/c/g;x')
  59. checkJoin('g;x?y#s', 'http://a/b/c/g;x?y#s')
  60. checkJoin('.', 'http://a/b/c/')
  61. checkJoin('./', 'http://a/b/c/')
  62. checkJoin('..', 'http://a/b/')
  63. checkJoin('../', 'http://a/b/')
  64. checkJoin('../g', 'http://a/b/g')
  65. checkJoin('../..', 'http://a/')
  66. checkJoin('../../', 'http://a/')
  67. checkJoin('../../g', 'http://a/g')
  68.  
  69. # "abnormal" cases from RFC 1808:
  70. checkJoin('', 'http://a/b/c/d;p?q#f')
  71. checkJoin('../../../g', 'http://a/../g')
  72. checkJoin('../../../../g', 'http://a/../../g')
  73. checkJoin('/./g', 'http://a/./g')
  74. checkJoin('/../g', 'http://a/../g')
  75. checkJoin('g.', 'http://a/b/c/g.')
  76. checkJoin('.g', 'http://a/b/c/.g')
  77. checkJoin('g..', 'http://a/b/c/g..')
  78. checkJoin('..g', 'http://a/b/c/..g')
  79. checkJoin('./../g', 'http://a/b/g')
  80. checkJoin('./g/.', 'http://a/b/c/g/')
  81. checkJoin('g/./h', 'http://a/b/c/g/h')
  82. checkJoin('g/../h', 'http://a/b/c/h')
  83.  
  84. # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
  85. # so we'll not actually run these tests (which expect 1808 behavior).
  86. #checkJoin('http:g', 'http:g')
  87. #checkJoin('http:', 'http:')
  88.  
  89. print errors, "errors"
  90.  
  91. # One more test backported from 2.3
  92. for u in ['Python', './Python']:
  93.     if urlparse.urlunparse(urlparse.urlparse(u)) != u:
  94.         print "*** urlparse/urlunparse failure for", `u`
  95.